<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="cancel">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <!-- 选项 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id"> {{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="saveRole">确定</el-button>
        <el-button size="small" @click="cancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleListApi } from '@/api/setting'
import { getUserDetailByIdApi } from '@/api/user'
import { assignRolesApi } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false,
    },
    userId: {
      type: String,
    },
  },
  data() {
    return {
      list: [], // 放所有角色列表
      checkList: ['选中且禁用', '复选框 A', '复选框 B'],
      roleIds: [], // 当前用户已有的角色列表
    }
  },

  mounted() {
    this.getRoleList()
  },

  methods: {
    async getRoleList() {
      const { rows } = await getRoleListApi()
      this.list = rows
    },
    cancel() {
      // 取消弹窗
      this.roleIds = []
      this.$emit('update:showRoleDialog', false)
    },
    // 获取当前用户角色列表
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailByIdApi(id)
      this.roleIds = roleIds // 赋值本用户的角色
    },
    async saveRole() {
      await assignRolesApi({ id: this.userId, roleIds: this.roleIds })
      this.$emit('update:showRoleDialog', false)
    },
  },
}
</script>

<style lang="scss" scoped></style>
